package com.cn.xiongmeng.czlxz.ui;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cn.xiongmeng.czlxz.R;
import com.cn.xiongmeng.czlxz.activity.WorkActivity;

public class VictoryFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "VictoryFragment";
    WorkActivity activity;

    public VictoryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach");
        // 获取Activity实例
        activity = (WorkActivity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView =  inflater.inflate(R.layout.fragment_victory, container, false);

        ImageView imgBackToIndex = rootView.findViewById(R.id.back_to_index);
        ImageView imgNextLevel = rootView.findViewById(R.id.next_level);

        imgBackToIndex.setOnClickListener(this);
        imgNextLevel.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v){
        if (v.getId() == R.id.back_to_index){
            Log.d(TAG, "点击了返回主页");
            activity.finish();
        } else if (v.getId() == R.id.next_level) {
            Log.d(TAG, "点击了下一关");

            activity.wenzhangFragment.init();
            activity.init();

            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            // 隐藏Fragment
            if (this.isAdded()) {
                fragmentTransaction.hide(this);
            }
            fragmentTransaction.commit();
        }
    }
}